﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSharpLinqTeach
{
    internal class TestClass1
    {
        #region  推断类型var
        //推断类型var
        public static void Demo1()
        {
            //int num = 100;
            var a = 20;
            var b = "上位机";
            var c = new Equipment
            {
                EquipmentId = 1,
                EquipmentName = "PLC"
            };

            var d = new Equipment
            {
                EquipmentId = 2,
                EquipmentName = "变频器"
            };

            Console.WriteLine(a.GetType());
            Console.WriteLine(b.GetType());
            Console.WriteLine(c.GetType());
            Console.WriteLine(d.GetType());
        }

        public static void Demo2()
        {
            object a = 100;
            a = "thinger";

            var b = 100.09;
            //b = "thinger" //这个会出现错误，原因是b在上面已经被推断为浮点类型
        }

        //匿名类 (匿名对象)   特点：就是我们要创建一个对象，并不知道这个类名字是什么
        //                    对象里面的属性，可以根据需要添加
        //                    不需要提前定义类

        //结论：var不仅可以用来定义普通的变量，重点是用来定义匿名对象
        public static void Demo3()
        {
            var equipment = new
            {
                EquipId = 2,
                EquipName = "变频器",
                EquipType = "设备主机"
            };

            Console.WriteLine($"{equipment.EquipId} {equipment.EquipName} {equipment.EquipType}");
        }
        #endregion

        #region 扩展方法的使用
        public static void Demo4()
        {
            int temp1 = 20;
            int temp2 = 21;
            int temp3 = 25;
            int sumTemp = temp1 + temp2 + temp3;

            //int avgTemp = sumTemp / 3;
            int avgTemp = sumTemp.Divistion(3);

            Equipment obj = new Equipment
            {
                EquipmentId = 1,
                EquipmentName = "变频器",
                EquipmentType = "常用对象"
            };
            string res = obj.ShowInfo("设备名称", "设备类型");
            Console.WriteLine(res);
        }
        #endregion

        #region 匿名方法和Lambda表达式
        public static int Method(int a, int b)
        {
            return a + b;
        }

        public static void Demo5()
        {
            //定义委托变量
            Func<int, int, int> cal = Method;

            //使用委托变量
            int res = cal(10, 20);
            Console.WriteLine(res);
        }

        public static void Demo6()
        {
            Func<int, int, int> cal = delegate (int a, int b)
            {
                return a + b;
            };

            //使用委托变量
            int res = cal(10, 20);
            Console.WriteLine(res);
        }

        public static void Demo7()
        {
            Func<int, int, int> cal =  (int a, int b) =>
            {
                return a + b;
            };

            //使用委托变量
            int res = cal(10, 20);
            Console.WriteLine(res);


            //可以使用推断类型
            Func<int, int, int> cal2 = (a, b) =>
            {
                return a + b;
            };

            //如果是一个参数，可以不用括号
            Func<int, int> cal3 = a => { return a * a; };

            //无参数的系统委托和Lambda表达式
            Action test = () => { Console.WriteLine("我们正在学习和巩固Lambda表达式"); };
        }
        #endregion
    }
}
